import json
import logging.handlers
import os
from  bs4 import BeautifulSoup


from config import DIR_PATH

def parser_html(result):
    # 1、提取html
    html = result.json().get("description").get("form")
    # 2、获取bs对象
    bs = BeautifulSoup(html, "html.parser")
    # 3、提取url
    url = bs.form.get("action")
    data = {}
    # 4、查找所有的input标签
    for input in bs.find_all("input"):
        data[input.get("name")] = input.get("value")
    return url, data


def read_json(filename, key):

    file_path = DIR_PATH + os.sep + "data" + os.sep + filename
    arr = []
    with open(file_path, "r", encoding="utf-8") as f:
        for data in json.load(f).get(key):
            arr.append(tuple(data.values())[1:])
        return arr

class GetLog:
    @classmethod
    def get_log(cls):
        cls.log =None
        if cls.log is None:
        # 1、获取⽇志器
            cls.log = logging.getLogger()
        # 设置⽇志级别 info
            cls.log.setLevel(logging.INFO)
            filepath = DIR_PATH + os.sep + "log" + os.sep + "p2p.log"
        # 2、获取处理器 TimedRotatingFileHandler：⽇志保存到⽂件且根据时间去分割
            tf = logging.handlers.TimedRotatingFileHandler(filename=filepath,
                                                      when="midnight",
                                                      interval=1,
                                                      backupCount=3,
                                                      encoding="utf-8"
                                                      )
            # 3、获取格式器
            fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
            fm = logging.Formatter(fmt)
            # 4、将格式器添加到处理器中
            tf.setFormatter(fm)
            # 5、将处理器添加到⽇志器中
            cls.log.addHandler(tf)

        # 返回⽇志器
        return cls.log







